The Web Scraping API allows developers to use a structured format to scrap website data. Returns website real-time data dependent on the web page URL defined in the settings of the API. The Web Scraping API is very helpful to extract content from the HTML source of the web pages.
Various web scraping APIs for scraping the information on the website are available. Among these, Scrapestack is one of the best web scraping free APIs. Scrapestack provides a user-friendly REST API that collects information from a website with IP frames, CAPTCHA or geolocations without programming or constraint. We’ll show you how to integrate Web Scraping API with PHP’s Scrapestack REST API in this tutorial.
To integrate Web Scraping API with scrapestack in PHP, follow the below simple steps.
Getting Started to Get Scrapstack API Access Key
- Create an account on scrapestack.
- In the dashboard, you will get the API key under the Your API Access Key.
Scrapestack API Configuration
The Access Key is required to authenticate and access the scrapestack API
2 3 4 5 6 7 8 |
// Build the query string using http_build_query() function to pass required params in the scrapestack API. $queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'url' => 'http://scrapestack.com', ]); |
After a successful API request, the webpage content will be returned in a structured format.
2 3 4 5 |
// Render website content echo $website_content; |
256-bit HTTPS Encryption
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with https
.
2 3 4 |
https://api.scrapestack.com/scrape |
API Error Codes
If your request fails, the scrapestack API will return an error in JSON format. Find below an example error that occurs when the API failed scraping the requested URL.
2 3 4 5 6 7 8 9 10 11 |
// Error Code { "success": false, "error": { "code": 105, "type": "scrape_request_failed" } } |
Common API Errors:
Code | Type | Info |
---|---|---|
404 | 404_not_found | User requested a resource which does not exist. |
101 | missing_access_key | User did not supply an access key. |
101 | invalid_access_key | User supplied an invalid access key. |
102 | inactive_user | User account is inactive or blocked. |
103 | invalid_api_function | User requested a non-existent API function. |
104 | usage_limit_reached | User has reached his subscription’s monthly request allowance. |
105 | function_access_restricted | The user’s current subscription does not support this API function. |
105 | https_access_restricted | The user’s current subscription plan does not support HTTPS. |
210 | missing_url | User has not specified a valid URL to scrape. |
211 | invalid_url | User has specified an invalid value in the URL parameter. |
212 | invalid_proxy_location | User has specified an invalid or unsupported proxy location. |
213 | scrape_request_failed | The current scraping request failed due to a technical issue. If this error occurs, please report this to technical customer support. |
Code Example – PHP script to scrape Content from Website via scrapestack API
Below you will find complete code to extract webpage content using PHP.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $queryString = http_build_query([ 'access_key' => 'YOUR_ACCESS_KEY', 'url' => 'http://scrapestack.com', ]); $ch = curl_init(sprintf('%s?%s', 'http://api.scrapestack.com/scrape', $queryString)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $website_content = curl_exec($ch); curl_close($ch); echo $website_content; |
Conclusion
The scrapestack API is free and premium plans for advanced uses are also available. We used some required parameters for calling the Web Scraping API in the example code. There are several configuration options available in the scrapestack API, which can be used to customize the scraping data. See the scrapestack API documentation for a complete reference.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co